package com.felhr.serialportexample.utils;

import java.text.DecimalFormat;

/**
 * 功能: 解析数据工具栏
 * <p>
 * Author: longyg
 * Date: 2019/2/26 0026.
 * Email 1020231698@qq.com
 */
public class DataUtils {

    public static String pareData(String msg) {
        if (null != msg && msg.length() >= 9) {
            try {
                return new DecimalFormat("#0.000").format(Integer.parseInt(msg.substring(3, 9)) / 1000.000) + " kg";
            } catch (Exception e) {
                return "0.00 kg";
            }
        }
        return "0.00 kg\n";
    }

    public static String pareDataNoUnit(String msg) {
        if (null != msg && msg.length() >= 9) {
            try {
                return new DecimalFormat("#0.000").format(Integer.parseInt(msg.substring(3, 9)) / 1000.000) + "";
            } catch (Exception e) {
                return "0.00";
            }
        }
        return "0.00";
    }
}